//
//  Tabbar.swift
//  myWeibo
//
//  Created by 章发晨 on 2017/3/6.
//  Copyright © 2017年 章发晨. All rights reserved.
//

import UIKit

class Tabbar: UITabBar {
    // 加号按钮点击要执行的闭包
    var composeButtonClickClosure: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //加好按钮放到中间
        composeButton.center = CGPointMake(self.frame.width * 0.5, self.frame.height * 0.5)
        
        //调节其他按钮的位置
        let width = self.frame.width/5
        
        var index = 0
        
        
        
        //遍历获得其他按钮
        for value in self.subviews{
//            UITabBarButton
            //判断
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                value.frame.size.width = width
                //设置每个按钮的位置,x值
                value.frame.origin.x = CGFloat(index) * width
                index += 1
                //到了第三个按钮的时候在往后移动一个
                if index == 2 {
                    index += 1
                }
             }
 
        }
        
    }
    
    // MARK: - 关联方法
    func composeButtonClick() {
        composeButtonClickClosure?()
    }
    
    
    
    // MARK: - 懒加载控件
    //懒加载按钮控件
    private lazy var composeButton: UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action:#selector(Tabbar.composeButtonClick), forControlEvents: .TouchUpInside)
        
        //分状态设置图片和背景图片
        // 设置不同状态下的图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        // 设置不同状态下的背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置按钮大小
        button.sizeToFit()
        return button
    }()

    

}
